<template name="loginPop">
	<view class="uni-tip borderR">
		<text class="uni-tip-title">欢迎您的到来</text>
		<text class="uni-tip-content">授权登录，获得完整购物体验~</text>
		<agreementAre class="agreement" :type="1" @check="agreementCheck"></agreementAre>
		<view class="uni-tip-group-button">
			<button class="uni-tip-button" @click="cancel('tip')">取消</button>
			<!-- <button class='uni-tip-button' style="color: white;" type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo"> -->
			<button class='uni-tip-button' style="color: white;" type="primary" @click="getUserProfileClick">
				授权
			</button>
		</view>
	</view>
</template>

<script>
	import util from '../common/util.js'
	import common from '../common/common.js'
	import {mapState,mapMutations} from 'vuex'
	import agreementAre from '@/components/agreementAre.vue'
	export default {
		name: 'loginPop',
		components: {agreementAre},
		data() {
			return {
				flag: false,
				encryptedData: '',
				iv: '',
				isCheck: true
			};
		},
		computed: {
			...mapState(['hasLogin'])
		},
		methods:{
			...mapMutations(['login','setLoginType','setUserId','setUserInfo','setOpenid','setIsShop','setShopId','setUnionId','setFId','setUserFId','setShopStatus']),
			cancel(type){
				this.$emit('cancel',type)
			},
			agreementCheck(list){
				let isCheck = true
				for(var i in list){
					if(!list[i].isCheck){
						isCheck = false
						break
					}
				}
				this.isCheck = isCheck
			},
			getUserProfileClick(){
				var that = this
				if(that.flag){
					return
				}
				that.flag = true
				if(!that.isCheck){
					util.showToast('none','请确认协议')
					that.flag = false
					return
				}
				uni.getUserProfile({
					desc: '用于获取用户头像', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (user) => {
						console.log(user);
						if (user.userInfo) {
							that.encryptedData = user.encryptedData
							that.iv = user.iv
							that.setUserInfo(user.userInfo)
							if(!that.hasLogin){
								if(that.$store.state.openid && that.$store.state.unionid){
									util.postAjax(that.global.apiUrl + '/add_user',(res)=>{
										// console.log(res)
										if(res.data.code == '1'){
											that.login(that.$store.state)
											that.setUserId(res.data.user_id)
											that.setFId(res.data.fid)
											that.setUserFId(res.data.user_fid)
											that.setShopStatus(res.data.shop_status)
											if(that.global.shopUserId){//拉新
												common.setIntegral(that.global.shopUserId,4,1)
											}
										}else{
											util.showToast('none','数据出错，请重新尝试！')
										}
									},{nickname: that.$store.state.userInfo.nickName,openid: that.$store.state.openid,unionid: that.$store.state.unionid,headimgurl: that.$store.state.userInfo.avatarUrl,fid: that.global.shopId,user_fid: that.global.shopUserId,login_type: that.global.loginType})
								}else{
									that.getUser()
								}
							}
							that.cancel('tip')
							that.flag = false
						} else {
							//用户按了拒绝按钮
							that.cancel('tip')
							that.flag = false
						}
					},
					fail:(err)=>{
						that.flag = false
					}
				})
			},
			getUserInfo(e){
				var that = this
				if(that.flag){
					return
				}
				that.flag = true
				if(!that.isCheck){
					util.showToast('none','请确认协议')
					that.flag = false
					return
				}
				
				if (e.detail.userInfo) {
					that.encryptedData = e.detail.encryptedData
					that.iv = e.detail.iv
					that.setUserInfo(e.detail.userInfo)
					console.log(that.hasLogin);
					if(!that.hasLogin){
						if(that.$store.state.openid && that.$store.state.unionid){
							util.postAjax(that.global.apiUrl + '/add_user',(res)=>{
								if(res.data.code == '1'){
									that.login(that.$store.state)
									that.setUserId(res.data.user_id)
									that.setFId(res.data.fid)
									that.setUserFId(res.data.user_fid)
									that.setShopStatus(res.data.shop_status)
									if(that.global.shopUserId){//拉新
										common.setIntegral(that.global.shopUserId,4,1)
									}
								}else{
									util.showToast('none','数据出错，请重新尝试！')
								}
							},{nickname: that.$store.state.userInfo.nickName,openid: that.$store.state.openid,unionid: that.$store.state.unionid,headimgurl: that.$store.state.userInfo.avatarUrl,fid: that.global.shopId,user_fid: that.global.shopUserId,login_type: that.global.loginType})
						}else{
							that.getUser()
						}
					}
					that.cancel('tip')
					that.flag = false
				} else {
					//用户按了拒绝按钮
					that.cancel('tip')
					that.flag = false
				}
			},
			getUser(){
				var that = this
				if(!that.$store.state.loginType){
					return
				}
				// 登录
				uni.login({
					success:function(login){
						// 发送请求
						uni.request({
							url: that.global.apiUrl + '/Api/GetUser/get_openid_one', //接口地址
							data: {
								code: login.code,
								encryptedData: encodeURIComponent(that.encryptedData),
								iv: encodeURIComponent(that.iv),
								type: that.$store.state.loginType,
								user_id: that.$store.state.userId
							},
							method: 'get',
							header: {
								'content-type': 'application/json' //默认值
							},
							success: function (res) {
								//判断用户是否存在
								if(res.data.code == '1'){
									//后台有数据
									that.login(that.$store.state)
									that.setUserId(res.data.data.id)
									that.setIsShop(res.data.data.is_shop)
									that.setShopId(res.data.data.user_shop_id)
									that.setOpenid(res.data.data.openid)
									that.setUnionId(res.data.data.unionid)
									that.setFId(res.data.data.fid)
									that.setUserFId(res.data.data.user_fid)
									that.setShopStatus(res.data.data.shop_status)
									that.setLoginType('')
								}else if(res.data.code == '0'){
									util.showToast()
								}else if(res.data.code == '2'){
									that.setOpenid(res.data.data.openId)
									that.setUnionId(res.data.data.unionId)
									that.setLoginType('')
									util.postAjax(that.global.apiUrl + '/add_user',(add)=>{
										if(add.data.code == '1'){
											that.login(that.$store.state)
											that.setUserId(add.data.user_id)
											that.setFId(add.data.fid)
											that.setUserFId(add.data.user_fid)
											that.setShopStatus(add.data.shop_status)
											if(that.global.shopUserId){//拉新
												common.setIntegral(that.global.shopUserId,4,1)
											}
										}else{
											util.showToast('none','数据出错，请重新尝试！')
										}
									},{nickname: that.$store.state.userInfo.nickName,openid: that.$store.state.openid,unionid: that.$store.state.unionid,headimgurl: that.$store.state.userInfo.avatarUrl,fid: that.global.shopId,user_fid: that.global.shopUserId,login_type: that.global.loginType})
								}
								
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	
	/* 提示窗口 */
	.uni-tip {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-direction: column;
		/* #endif */
		@include widthHeight(550rpx,400rpx);
		@include padding(30rpx);
		@include backG;
		@include justify-content('b');
		.uni-tip-title {
			@include text-align;
			@include font($fontSize + 4rpx,$blackColor);
			font-weight: bold;
		}
		.uni-tip-content {
			@include text-align;
			@include font($fontSize,$greyColor);
		}
		.uni-tip-group-button {
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			flex-direction: row;
			.uni-tip-button {
				flex: 1;
				@include text-align;
				@include font($fontSize,$lightGreyColor);
			}
		}
		
	}
</style>
